using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace QMVnSoft.Controls
{
    public partial class FormControl1 : DevComponents.DotNetBar.Office2007Form
    {        
        private Keys ik_UserLastKey;
        private DataGridView oActiveGridBrowser;

        public FormControl1()
        {            
            this.InitializeComponent();
        }      

        private object GetActiveValue()
        {
            Control userActiveControl = this.GetUserActiveControl();
            if ((userActiveControl is DataGridView) && ((userActiveControl as DataGridView).CurrentCell != null))
            {
                return (userActiveControl as DataGridView).CurrentCell.Value;
            }
            if (userActiveControl is TextBox)
            {
                return (userActiveControl as TextBox).Text;
            }
            return null;
        }

        public Control GetUserActiveControl()
        {
            Control activeControl = base.ActiveControl;
            while (activeControl is ContainerControl)
            {
                activeControl = (activeControl as ContainerControl).ActiveControl;
            }
            return activeControl;
        }       

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            this.UserLastKey = keyData;
            if (((msg.Msg == 0x100) && (keyData == Keys.Return)) && !(this.GetUserActiveControl() is Button))
            {
                if (this.GetUserActiveControl() is TextBox)
                {
                    if ((this.GetUserActiveControl() as TextBox).Multiline)
                    {
                        return base.ProcessCmdKey(ref msg, keyData);
                    }
                    SendKeys.Send("{TAB}");
                }
                else
                {
                    SendKeys.Send("{TAB}");
                }
                return true;
            }
            if ((msg.Msg == 0x100) && (keyData == Keys.F5))
            {
                AppInstance.UserCalculator.CurrentControl = this.GetUserActiveControl();
                AppInstance.UserCalculator.SetTextValue(this.GetActiveValue());
                AppInstance.UserCalculator.Show();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        [Browsable(false)]
        public DataGridView ActiveGridBrowser
        {
            get
            {
                return this.oActiveGridBrowser;
            }
            set
            {
                if (!this.CheckSearchFocused && (this.oActiveGridBrowser != value))
                {
                    this.oActiveGridBrowser = value;
                }
            }
        }

        [Browsable(false)]
        private bool CheckSearchFocused
        {
            get
            {
                return ((this.GetUserActiveControl() != null) && (this.GetUserActiveControl() is TextBox));
            }
        }

        [Browsable(false)]
        public Keys UserLastKey
        {
            get
            {
                return this.ik_UserLastKey;
            }
            set
            {
                this.ik_UserLastKey = value;
            }
        }

        protected void ResizeForm()
        {            
            Screen scrn = Screen.FromControl(this);
            if (scrn == null)
            {
                scrn = Screen.PrimaryScreen;
            }
            int deskHeight = scrn.Bounds.Height;
            int deskWidth = scrn.Bounds.Width;
            if (deskWidth < 1000)
            {                
                this.Height = 577;
                this.Width = 810;
            }
            else
            {
                this.Width = 924;
            }
        }
    }
    }